package com.netcombine.freewill.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.netcombine.freewill.constants.PageInfo;
import com.netcombine.freewill.lib.Paging;
import com.netcombine.freewill.manager.SessionManager;
import com.netcombine.freewill.service.HelpService;
import com.netcombine.freewill.vo.FreewillVo;

@Controller
public class HelpController{

	private static final Logger logger = LoggerFactory.getLogger(HelpController.class);	
	
	@Autowired
	private HelpService helpService;
	
	@RequestMapping(value = "/help/list.do")
	public String helplist(FreewillVo vo, HttpSession session, Model model){		
		
		if(!SessionManager.isLogin(session)){
			
			return PageInfo.GO_LOGIN;
		}		
		if(vo.getPage() == 0) vo.setPage(1);
		int total_cnt = helpService.selectHelpCount(vo);
		logger.info("total_cnt : " + Integer.toString(total_cnt));
	
		Paging paging = new Paging(vo.getPage(), total_cnt, PageInfo.PAGE_SIZE, PageInfo.BLOCK_PAGE, PageInfo.PAGING_SCRIPT);
		
		if(total_cnt != 0){
		
		// paging setting
			vo.setPageSize(paging.getPageSize());
			vo.setStartPosition(paging.getPageStart());			
		
			List<FreewillVo> helpList = helpService.selectHelpList(vo);
		
			model.addAttribute("freewillVo", vo);
			model.addAttribute("helpList", helpList);
		}
			model.addAttribute(PageInfo.CURRENT_MENU, PageInfo.MENU_USER);
			model.addAttribute("paging", paging);
			
			return "help/list";
	}	
}